<?php
//暂时用不到
namespace Common\Controller;
use Think\Controller;

class PayController extends Controller{
//下单成功之后的相关逻辑  用的common



    public function paysuccess( $orderid,$userid)
    {   
      
       $jifen_xf_one= M('jifen')->getField('jifen_xf_one');

       $orderinfo= M('order')->find($orderid);
        $data['id']=$orderid;
        $data['paystatus']=1;
        $data['audit_time']=time();

        
        $jfinfo=M('jifen')->find();
         M()->startTrans();
         $res1=M('order')->save($data);
         $add_xfjifen=$orderinfo['moshi_total_xf'];
         $res2=M('user')->where("id=$userid")->setInc("xf_jifen",$add_xfjifen);
         $res3=M('user')->where("id=$userid")->setInc("ordernums",$orderinfo['order_num']);
         $record['user_id']=$userid;
         $record['items']='下单奖励积分';
         $record['jifen_num']='+'.$add_xfjifen;
         $record['addtime']=time();
         $record['jifen_type']='xf';
         M('jifen_record')->add($record);
        $status=M('user')->where("id=$userid")->getField('status');
    

         $map['user_id']=$userid;
         $map['xf_status']=0;
         $map['paystatus']=1;
         $orderxf_num=D('order')->getTotalNums($map);
            //自身消费满10单，送上级积分
       if ($orderxf_num>=10) {
             $invite_id=M('user')->where("id=$userid")->getField('invite_id');
          M('order')->where($map)->setField('xf_status',1);
          unset($record);
          $record['user_id']=$invite_id;
         $record['items']='您邀请的好友超10单奖励积分';
         $record['jifen_num']='+'.$jfinfo['jifen_xf_ten'];
         $record['addtime']=time();
         $record['jifen_type']='xf';
          $record['from_userid']=$userid;
         M('jifen_record')->add($record);
          M('user')->where("id=$invite_id")->setInc("xf_jifen",$jfinfo['jifen_xf_ten']);
        }

 

        //获取上级id 自身团队
        $invite_id=M('user')->where("id=$userid")->getField('invite_id');
        if ($invite_id) {
             //是否达到自动解绑 
            $iteration =D('Order')->getOrderNums_up($userid,$orderinfo['order_num']); 
            $invite_status=M('user')->where("id=$invite_id")->getField('status');
             if ($self_status>=$invite_status ) {

                 if ($up_status==$up_up_status) {
                             M('user')->where('id='.$userid)->setField('f_id',0);
                          }else{
                             M('user')->where('id='.$userid)->setField('f_id',-1);
                             $beyond_months= M('jifen')->getField('beyond_months');
                          $beyond['beyond_end_time']= strtotime("+".$beyond_months."months");
                          $beyond['beyond_sj_id']=$invite_id;
                          $beyond['beyond_time']=time();
                      M('user')->where("id=$userid")->save($beyond);
                              $c=M('user')->where("invite_id=$userid")->select();
                        foreach ($c as $key => $value) {
                           if ($value['status']<$up_status) {
                                  M('user')->where('id='.$value['id'])->setField('f_id',$value['invite_id']);
                            } 
                        }

                          }
                
            }

           
        
        $end=time();
         $begin=mktime(0,0,0,date('m')-2,1,date('Y'));

        $getOrderNums_team =D('Order')->getTeamTotalNumsUpCum($userid,$begin,$end);


        }
        //消费者以上自身消费满10单
       if ($status>1) {
        unset($map);
        //自身消费满10单的条件
        $map['user_id']=$userid;
        $map['paystatus']=1;
     
        $self_ordernum=D('order')->getTotalNums($map);
        $team_ordernum=D('order')->getTeamTotalNumsNow($userid);

        if ($self_ordernum==10) {
            D('order')->where($map)->setField('self_gz_status',1);
        }
              
    }


         if ($res2 && $res1 && $res3) {
            M()->commit(); 
            return true;
         }else{
           M()->rollback();
           return false;
         }

    }


    
}